<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
        <span>重置密码</span>
    </div>
        <el-form :model="pwdForm" status-icon :rules="rules" ref="pwdForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="原密码" prop="old_pwd">
                <el-input type="password" v-model="pwdForm.old_pwd"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="new_pwd">
                <el-input type="password" v-model="pwdForm.new_pwd"></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="re_pwd">
                <el-input type="password" v-model="pwdForm.re_pwd"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm">提交</el-button>
                <el-button @click="resetForm">重置</el-button>
            </el-form-item>
        </el-form>
  </el-card>
</template>

<script>
import { updateUserPwdAPI } from '@/api'
export default {
  data () {
    const samePwd = (rule, value, callback) => {
      if (value === this.pwdForm.old_pwd) {
        callback(new Error('新旧密码不能相同'))
      } else {
        callback()
      }
    }
    const rePwd = (rules, value, callback) => {
      if (value !== this.pwdForm.new_pwd) {
        callback(new Error('密码不一致请重新输入'))
      } else {
        callback()
      }
    }
    return {
      pwdForm: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      rules: {
        old_pwd: [
          {
            required: true,
            message: '请输入旧密码',
            trigger: 'blur'
          },
          {
            pattern: /^\S{6,10}$/,
            message: '密码为6-10位',
            trigger: 'blur'
          }
        ],
        new_pwd: [
          {
            required: true,
            message: '请输入新密码',
            trigger: 'blur'
          },
          {
            pattern: /^\S{6,10}$/,
            message: '密码为6-10位',
            trigger: 'blur'
          },
          {
            validator: samePwd,
            trigger: 'blur'
          }
        ],
        re_pwd: [
          {
            required: true,
            message: '请输入新密码',
            trigger: 'blur'
          },
          {
            validator: rePwd,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    async submitForm () {
      this.$refs.pwdForm.validate(async (valid) => {
        if (valid) {
          const { data: res } = await updateUserPwdAPI(this.pwdForm)
          if (res.code !== 0) {
            this.$message.error(res.message)
          }
          this.$message.success(res.message)
          this.resetForm()
          this.$store.commit('updateToken', '')
          this.$store.commit('updateUserInfo', {})
          this.$router.push('/login')
        } else {
          return false
        }
      })
    },
    resetForm () {
      this.$refs.pwdForm.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.box-card{
    height: 500px;
  .demo-ruleForm{
    width: 500px;
  }
}
</style>
